package com.taotao.service.impl;

import com.taotao.common.pojo.EUTreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zhaoliang on 2017/7/16.
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<EUTreeNode> getCategoryList(long parentId) {
        //根据parentid查询节点列表
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //执行查询
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
        List<EUTreeNode> resultList = new ArrayList<>();
        for (TbContentCategory tbContentCategory : list) {
            //创建一个节点
            EUTreeNode node = new EUTreeNode();
            node.setId(tbContentCategory.getId());
            node.setText(tbContentCategory.getName());
            node.setState(tbContentCategory.getIsParent()?"closed":"open");

            resultList.add(node);
        }
        return resultList;
    }

    @Override
        public TaotaoResult insertContentCategory(long parentId, String name){

            //创建一个pojo
            TbContentCategory contentCategory = new TbContentCategory();
            contentCategory.setName(name);
            contentCategory.setIsParent(false);
            //'状态。可选值:1(正常),2(删除)',
            contentCategory.setStatus(1);
            contentCategory.setParentId(parentId);
            contentCategory.setSortOrder(1);
            contentCategory.setCreated(new Date());
            contentCategory.setUpdated(new Date());
            //添加记录
            contentCategoryMapper.insert(contentCategory);
            //查看父节点的isParent列是否为true，如果不是true改成true
            TbContentCategory parentCat = contentCategoryMapper.selectByPrimaryKey(parentId);
            //判断是否为true
            if (!parentCat.getIsParent()) {
                parentCat.setIsParent(true);
                //更新父节点
                contentCategoryMapper.updateByPrimaryKey(parentCat);
            }
            //返回结果
            return TaotaoResult.ok(contentCategory);
        }

    @Override
    public TaotaoResult deleteContentCategory(Long parentId, Long id) {

        //根据parentid查询节点列表
        Long parId = parentId;
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        if(parentId == null){
            TbContentCategory parentCat = contentCategoryMapper.selectByPrimaryKey(id);
            parId = parentCat.getParentId();
            criteria.andParentIdEqualTo(parId);
        }else {
            criteria.andParentIdEqualTo(parId);
        }
        //执行查询
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
        if(list.size()==1){
            //查看父节点的isParent
            TbContentCategory parentCat = contentCategoryMapper.selectByPrimaryKey(parId);
            parentCat.setIsParent(false);
            //更新父节点
            contentCategoryMapper.updateByPrimaryKey(parentCat);
        }
       //删除此条记录
        contentCategoryMapper.deleteByPrimaryKey(id);

        //返回结果
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult updateContentCategory(Long id,String name) {
        TbContentCategory parentCat = contentCategoryMapper.selectByPrimaryKey(id);
        parentCat.setName(name);
        //更新父节点
        contentCategoryMapper.updateByPrimaryKey(parentCat);

        //返回结果
        return TaotaoResult.ok();
    }
}
